package com.lovo.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQSConfig {

    //spring封装rabiitmq的代码 RabbitTemplate
//注入工厂（已经预加载）
    @Autowired
    CachingConnectionFactory connectionFactory;

    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory);
    }

    //供货单 队列
    @Bean
    public Queue supplierMsgQueue() {
        return new Queue("supplierMsgQueue");
    }

    //投标 队列
    @Bean
    public Queue tenderInfoQueue() {
        return new Queue("tenderInfoQueue");
    }

    //供货单 交换机
    @Bean
    public DirectExchange directExchangeMsg() {
        return new DirectExchange("directExchangeMsg");
    }

    //投标 交换机
    @Bean
    public DirectExchange directExchangeInfo() {
        return new DirectExchange("directExchangeInfo");
    }

    // 供货单 交换机与队列进行绑定
    @Bean
    public Binding supplierMsgQueueToDirectExchangeMsg(Queue supplierMsgQueue, DirectExchange directExchangeMsg) {
        return BindingBuilder.bind(supplierMsgQueue).to(directExchangeMsg).with("msg.key");
    }

    // 投标 交换机与队列进行绑定
    @Bean
    public Binding tenderInfoQueueToDirectExchangeInfo(Queue tenderInfoQueue, DirectExchange directExchangeInfo) {
        return BindingBuilder.bind(tenderInfoQueue).to(directExchangeInfo).with("info.key");
    }


}
